<?php

namespace app\admin\controller;

use think\Db;
use app\common\controller\Backend;
use app\admin\model\BydDesignerArea;
/**
 * 楼盘信息
 *
 * @icon fa fa-circle-o
 */
class SoleBuilding extends Backend
{
    
    /**
     * SoleBuilding模型对象
     * @var \app\admin\model\SoleBuilding
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\SoleBuilding;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

     /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $name=$params['title'];
                $bz=$params['standard'];
                $adedress=$params['address'];
                $time=$params['trun_time'];
                $huxin=$params['house_type'].$params['house_area'];
                $image=[
                    'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F09%2F3a%2Fbc%2F093abce7b31f4c8ffdbf345375ff4abb.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649666490&t=80e0fdd6612e35c771dc378e3d043d28',
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F42679d1a2a49e8419cfa88a5d029b8fa4091db5249b7a-JN8fIB_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649666516&t=0dcf1f472395107e9f9f10dceafb4042" ,
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2F2%2F59ccc47e4623f.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649666516&t=55bd62b24de60dc90bab30edd352b600"
                ];
                $path= APP_PATH.'api/view/demo/abc.html';
                $opempath=fopen($path,'a+');
                $arr=fread($opempath,filesize($path));
                $arr=str_replace('{$name}',$name,$arr);
                $arr=str_replace('{$bz}',$bz,$arr);
                $arr=str_replace('{$address}',$adedress,$arr);
                $arr=str_replace('{$time}',$time,$arr);
                $arr=str_replace('{$huxin}',$huxin,$arr);
                $imagestring='';
                foreach ($image as $v){
                    $imagestring.='<img src='.$v.'>';
                }
                $arr=str_replace('{$image}',$imagestring,$arr);
                $params['content']=$arr;
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                
                if(intval($params['area_id']) <= 0) $this->error(__('请选择楼盘所属区域'));
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    // print_r($params);
                    // die;
                    $params['create_time'] = time();
                    $params['update_time'] = time();
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {

                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }



     
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        print_r($adminIds);
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $params['update_time'] = time();
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty'));
        }
        
        $row =  $row->toArray();
        $res = BydDesignerArea::get($row['area_id']);
     
        if(!empty($res) && $res['level'] = 2)
        {
            $city = BydDesignerArea::where('id','=',$res->pid)->find();
            $row['city_id'] = $city->id;
            $row['province_id'] = $city->pid;
         
        }
        else
        {
            $row['province_id'] = 0;
            $row['city_id'] = 0;
        }
        
        
        
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
